module nade.location;

import derelict.opengl.gl;

import nade.vector;
import nade.angle;
import nade.matrix;


class Locatable {
	void attached() { }
	void moved() { }

	Location location() { return _location; }

	private {
		Location			_location;
	}
}


class Location {
	this(const Vector2 position = Vector2.ZERO, const Radians angle = Radians.ZERO)
	{
		_position = position;
		_angle = angle;
	}
	this(const Vector2 position, const Degrees angle)
	{
		_position = position;
		_angle = angle.to!(Radians);
	}

	void render(void delegate() dg) const
	{
		glPushMatrix();
		glTranslatef(position.x, position.y, 0);
		glRotatef(angle.degrees, 0, 0, 1);

		dg();

		glPopMatrix();
	}

	Location attach(Locatable object)
	{
		object._location = this;
		_objects ~= object;
		object.attached;

		return this;
	}

	Matrix2x3 toMatrix2x3() const { return Matrix2x3.transformation(_position, _angle);  }

	void move(const Vector2 delta, const Radians adelta)
	{
		moveTo(_position + delta, _angle + adelta);
	}
	void moveTo(const Vector2 position, const Radians angle)
	{
		_position = position;
		_angle = angle;
		foreach(object; _objects) object.moved;
	}

	Vector2 position() const { return _position; }
	Vector2 position(const Vector2 rhs) { return _position = rhs; }

	Radians angle() const { return _angle; }
	Radians angle(const Radians rhs) { return _angle = rhs; }

	private {
		Vector2			_position;
		Radians			_angle;
		Locatable[]		_objects;
	}
}
